package cn.demo.framework.aspectj;

import cn.demo.common.annotation.DataScope;
import cn.demo.common.core.domain.model.LoginUser;
import cn.demo.common.utils.*;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

import static cn.demo.common.constant.CommonConstants.DATA_SCOPE_ABLE;

/**
 * 数据过滤处理
 *
 * @author ruoyi
 */
@Aspect
@Component
public class DataScopeAspect {

    @Before("@annotation(dataScope)")
    public void doBefore(JoinPoint joinPoint, DataScope dataScope) {
        ServletUtils.removeAttr(DATA_SCOPE_ABLE);
        // 获取当前的用户, 如果是超级管理员，则不过滤数据
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (ObjUtils.isNotNull(loginUser) && ObjUtils.isNotNull(loginUser.getUserInfo()) && !loginUser.isAdmin()) {
            ServletUtils.setAttr(DATA_SCOPE_ABLE, true);
        }
    }
}
